package com.qububu.emporium.comment.control;

import static com.qububu.utils.ParamConverter.assertNotEmpty;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qububu.common.user.dto.UserInfoDto;
import com.qububu.emporium.comment.dto.GoodsCommentInfoDto;
import com.qububu.emporium.comment.service.CommentService;
import com.qububu.utils.DateUtil;
import com.qububu.utils.ParamConverter;
import com.qububu.utils.SystemConstants;
import com.qububu.utils.SystemUtil;
import com.qububu.utils.SystemValidate;

@Controller
@RequestMapping("/comment")
public class CommentControl {

	@Resource
	private CommentService commentService;
	
	/**
	 * @author HQ
	 * @功能 通过商品id查找商品评价详情
	 * @return detalMap 是返回的商品评价详情
	 * */
	@RequestMapping("/ajaxSearchComment")
	@ResponseBody
	public Map<String, Object> searchGoodsCommentsById(HttpServletRequest request) {
		
		Map<String, Object> commentsMap = new HashMap<String, Object>();
		
		String goodsId = assertNotEmpty(request.getParameter("goodsId"));
		String decGoodsId = SystemUtil.decrypt(goodsId);
		int realGoodsId = ParamConverter.convertToInteger(decGoodsId);
		
		List<GoodsCommentInfoDto> list = commentService.getGoodsCommentById(realGoodsId);

		commentsMap.put("result", SystemConstants.AJAX_SUCCESS);
		commentsMap.put("list", list);
		
		return commentsMap;
	}
	
	/**
	 * @author HQ
	 * @功能 通过商品id和用户id添加对商品的评价;当msgId存在时，是对评价详情的子评论
	 * @return mapObj为添加的对商品的评价
	 * */
	@RequestMapping("/addGoodsComment")
	@ResponseBody
	public Map<String, Object> addGoodsComment(HttpServletRequest request){
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		String msgId = SystemUtil.getUUIDString();
		
		String msgGoodsPoint = assertNotEmpty(request.getParameter("msgGoodsPoint") == null ? "0" : request.getParameter("msgGoodsPoint"));
		String msgContent = assertNotEmpty(request.getParameter("msgContent"));
		String msgCreateIp = SystemUtil.getRequestIp(request);
		
		//此处取出的goodsId和msgId是加密id
		String goodsId = assertNotEmpty(request.getParameter("goodsId"));
		String parentId = request.getParameter("msgId");

		if (SystemValidate.isEmpty(parentId)) {
			parentId = SystemUtil.encrypt("0");
		}
		
		//取得userId，从session里边
		UserInfoDto userInfoDto = (UserInfoDto) request.getSession().getAttribute(SystemConstants.LOGINUSER_SESSION_NAME);
		String userId = String.valueOf(userInfoDto.getUserId() );
		//此处得到商品图片传过来 的url，此方法是公共类里边写好的方法
		String msgContextPicUrl = SystemUtil.getSummaryPicUrl(msgContent, request.getContextPath());
		
		GoodsCommentInfoDto goodsCommentInfoDto = createGoodsCommentInfoDtoEntity(msgId, msgGoodsPoint, msgContent, msgContextPicUrl, msgCreateIp, goodsId, parentId, userId);
		
		GoodsCommentInfoDto returnGoodsCommentInfoDto = commentService.doAddGoodsComment(goodsCommentInfoDto);
		resultMap.put("result", SystemConstants.AJAX_SUCCESS);
		resultMap.put("goodsCommentInfoDto", returnGoodsCommentInfoDto);
		
		return resultMap;
	}
	
	/**
	 * @author HQ
	 * @功能 封装好GoodsCommentInfoDto类
	 * @param msgGoodsPoint 评论评分
	 * @param msgContent 评论内容
	 * @param msgGoodsPicUrl 评论内容中的图片
	 * @param msgCreateIp 评论人的id
	 * @param goodsId 商品id
	 * @param parentId 子评论时候的信息id
	 * @param userId 用户id
	 * @return goodsCommentInfoDto
	 */
	private GoodsCommentInfoDto createGoodsCommentInfoDtoEntity(String msgId, String msgGoodsPoint, String msgContent, String msgGoodsPicUrl, String msgCreateIp, String goodsId, String parentId, String userId){
		
		GoodsCommentInfoDto goodsCommentInfoDto = new GoodsCommentInfoDto(msgId, goodsId, userId, parentId);
		
		//得到系统当前时间的int值
		int nowTime = DateUtil.convertCurrentDTTMtoInt();

		//msgContent里边有非法字符，此处过滤
		msgContent = SystemUtil.convertSpecialMark(msgContent);
		
		goodsCommentInfoDto.setMsgGoodsPoint(msgGoodsPoint);
		goodsCommentInfoDto.setMsgContent(msgContent);
		goodsCommentInfoDto.setMsgCreateDttm(String.valueOf(nowTime));
//				showInfoDto.setMsgAgreeNum("0");
		goodsCommentInfoDto.setMsgGoodsPicUrl(msgGoodsPicUrl);
		goodsCommentInfoDto.setMsgCreateIp(msgCreateIp);
		
		return goodsCommentInfoDto;
	}
}
